package br.com.tenax.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import br.com.tenax.entidade.Funcionario;



public class FuncionarioDAOimpl implements FuncionarioDAO {

	public void incluir(Funcionario funcionario) {
		Connection conexao = null;
		try {
			conexao = DAO.getConexao();

			PreparedStatement pstm = conexao.prepareStatement("insert into "
					+ "tbfuncionario (id, nome, cpf, comissao, datainclusao, status, setor)  values (?,?,?,?,?,?,?)");
			pstm.setInt(1, funcionario.getId());
			pstm.setString(2, funcionario.getNome());
			pstm.setString(3, funcionario.getCpf());
			pstm.setString(4, funcionario.getComissao());
			pstm.setDate(5, (java.sql.Date) funcionario.getDataInclusao());
			pstm.setString(6, funcionario.getStatus());
			pstm.setString(7, funcionario.getSetor());
					
			pstm.executeUpdate();

			pstm.close();
			conexao.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void atualizar(Funcionario funcionario) {

		Connection conexao = null;
		try {
			conexao = DAO.getConexao();

			PreparedStatement pstm = conexao.prepareStatement("update "
					+ "tbfuncionario set nome = ?, cpf = ?, comissao = ?, datainclusao = ?, status = ?, setor = ?  where id =?");
			
			pstm.setString(1, funcionario.getNome());
			pstm.setString(2, funcionario.getCpf());
			pstm.setString(3, funcionario.getComissao());
			pstm.setDate(4, (java.sql.Date) funcionario.getDataInclusao());
			pstm.setString(5, funcionario.getStatus());
			pstm.setString(6, funcionario.getSetor());
			pstm.setInt(7, funcionario.getId());
			
			pstm.executeUpdate();

			pstm.close();
			conexao.close();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public void deletar(Funcionario funcionario) {
		Connection conexao = null;
		try {
			conexao = DAO.getConexao();

			PreparedStatement pstm = conexao.prepareStatement("delete from "
					+ "tbfuncionario where id =?");
			pstm.setInt(1, funcionario.getId());
			pstm.executeUpdate();

			pstm.close();
			conexao.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override 
	public Funcionario consultar(Funcionario funcionario) {
		Funcionario f = new Funcionario();
		Connection conexao = null;
		try {
			conexao = DAO.getConexao();
			PreparedStatement pstm = conexao.prepareStatement("Select * from "
					+ "tbfuncionario where id =?");
			pstm.setInt(1, funcionario.getId());
			ResultSet rs = pstm.executeQuery();
			while (rs.next()) {
				f.setId(rs.getInt("id"));
				f.setNome(rs.getString("nome"));
				f.setCpf(rs.getString("cpf"));
				f.setComissao(rs.getString("comissao"));
				f.setDataInclusao(rs.getDate("datainclusao"));
				f.setStatus(rs.getString("status"));
				f.setSetor(rs.getString("setor"));
				
				
			}
			pstm.close();
			conexao.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return f;
	}

	@Override
	public List<Funcionario> listar() {
		List<Funcionario> lista = new ArrayList<>();
		Connection conexao = null;
		try {
			conexao = DAO.getConexao();
			PreparedStatement pstm = conexao
					.prepareStatement("Select * from tbfuncionario ");

			ResultSet rs = pstm.executeQuery();
			while (rs.next()) {
				Funcionario f = new Funcionario();
				
				f.setId(rs.getInt("id"));
				f.setNome(rs.getString("nome"));
				f.setCpf(rs.getString("cpf"));
				f.setComissao(rs.getString("comissao"));
				f.setDataInclusao(rs.getDate("datainclusao"));
				f.setStatus(rs.getString("status"));
				f.setSetor(rs.getString("setor"));
				
				lista.add(f);
			}
			pstm.close();
			conexao.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lista;
	}
}